﻿package net.wangit.jdbc.table;

import net.wangit.Row;
import net.wangit.framework.config.BeanFactory;
import net.wangit.jdbc.generator.Generator;
import net.wangit.jdbc.generator.IncrementGenerator;
import net.wangit.jdbc.type.TypeFactory;
import net.wangit.util.Checker;
/**
 * 列工厂类
 * 
 * @author watson
 * Feb 12, 2010
 */
public class ColumnFactory {

	public ColumnFactory() {
	}

	public static final Column getColumn(String domain, Row row) {
		net.wangit.jdbc.Type type = TypeFactory.getType(row);
		Column column = new Column(type);
		column.setName(row.get("c"));
		column.setLabel(row.get("cl"));
		column.setNullable(row.getInt("nn", 0) == 0);
		String defaultValue = row.get("d");
		if (!Checker.isEmpty(defaultValue))
			column.setDefaultValue(defaultValue);
		String generatorId = row.get("g");
		if ("0".equals(generatorId))
			column.setAutoByDB(true);
		else if (!Checker.isEmpty(generatorId)) {
			Generator generator = getGenerator(domain, generatorId);
			column.setGenerator(generator);
			int generateRule = row.getInt("gr", 1);
			column.setGenerateRule(generateRule);
		}
		return column;
	}

	private static Generator getGenerator(String domain, String generatorId) {
		if (Checker.isInteger(generatorId))
			return BeanFactory.getGenerator(generatorId);
		else
			return new IncrementGenerator(domain, generatorId);
	}
}
